package com.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.News;
import com.base.po.voip.NewsAllocation;
import com.base.service.LogsService;
import com.base.service.NewsAllocationService;
import com.base.service.NewsService;
import com.base.utils.Page;

/**
 * admin管理系统控制器
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class content_xinwenliebiao {
	@Autowired
	private LogsService logsService;// log日志
	@Autowired
	private NewsService newsService;
	@Autowired
	private NewsAllocationService newsAllocationService;

	@RequestMapping("/content_xinwenliebiao")
	public ModelAndView index(HttpServletRequest request) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
		// 登录的用户ID 以及操作权限
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
		
		Integer toutiao = 0;
		Integer shehui = null;
		Integer guonei = null;
		Integer guoji = null;
		Integer yule = null;
		Integer tiyu = null;
		Integer junshi = null;
		Integer keji = null;
		Integer caijing = null;
		Integer shishang = null;
		Integer youxi = null;
		Integer qiche = null;
		Integer xiaohua = null;
		Integer jiankang = null;
		Integer jiaju = null;
		Integer xingzuo = null;
		Integer lishi = null;
		Integer zatan = null;
		NewsAllocation newsAllocation = newsAllocationService.selectByParentid(adminUserId);
		
		if(newsAllocation !=null) {
			if(newsAllocation.getShehui() == 1) {
				shehui = 1;
			}
			if(newsAllocation.getGuonei() == 1) {
				guonei = 2;
			}
			if(newsAllocation.getGuoji() == 1) {
				guoji = 3;
			}
		    if(newsAllocation.getYule() == 1) {
				yule = 4;
			}
		    if(newsAllocation.getTiyu() == 1) {
				tiyu = 5;
			}
		    if(newsAllocation.getJunshi() == 1) {
				junshi = 6;
			}
		    if(newsAllocation.getKeji() == 1) {
				keji = 7;
			}
		    if(newsAllocation.getCaijing() == 1) {
				caijing = 8;
			}
		    if(newsAllocation.getShishang() == 1) {
				shishang = 9;
			}
		    if(newsAllocation.getYouxi() == 1) {
				youxi = 10;
			}
		    if(newsAllocation.getQiche() == 1) {
				qiche = 11;
			}
		    if(newsAllocation.getXiaohua() == 1) {
				xiaohua = 12;
			}
		    if(newsAllocation.getJiankang() == 1) {
				jiankang = 13;
			}
		    if(newsAllocation.getJiaju() == 1) {
		    	jiaju = 14;
			}
		    if(newsAllocation.getXingzuo() == 1) {
		    	xingzuo = 15;
			}
		    if(newsAllocation.getLishi() == 1) {
		    	lishi = 16;
			}
		    if(newsAllocation.getZatan() == 1) {
		    	zatan = 17;
			}
		}
		page.setCurrentPageNo(1);
		page.setRecordTotal(newsService.countByCategory(toutiao, shehui, guonei, guoji, yule, tiyu, 
				junshi, keji, caijing, shishang, youxi, qiche, xiaohua, 
				jiankang, jiaju, xingzuo, lishi, zatan, null, null, null, null));
		List<News> news = newsService.selectByCategory(page, toutiao, shehui, guonei, guoji, yule,
				tiyu, junshi, keji, caijing, shishang, youxi, qiche, xiaohua, 
				jiankang, jiaju, xingzuo, lishi, zatan, null, null, null, null);
		for(News n : news) {
			switch (n.getCategory()) {
			case 0:
				n.setType("头条");
				break;
			case 1:
				n.setType("社会");
				break;
			case 2:
				n.setType("国内");
				break;
			case 3:
				n.setType("国际");
				break;
			case 4:
				n.setType("娱乐");
				break;
			case 5:
				n.setType("体育");
				break;
			case 6:
				n.setType("军事");
				break;
			case 7:
				n.setType("科技");
				break;
			case 8:
				n.setType("财经");
				break;
			case 9:
				n.setType("时尚");
				break;
			case 10:
				n.setType("电竞");
				break;
			case 11:
				n.setType("汽车");
				break;
			case 12:
				n.setType("笑话");
				break;
			case 13:
				n.setType("健康");
				break;
			case 14:
				n.setType("生活");
				break;
			case 15:
				n.setType("星座");
				break;
			case 16:
				n.setType("历史");
				break;
			case 17:
				n.setType("杂谈");
				break;
			default:
				break;
			}
		}
		/*
		 * 设置分页
		 */
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		request.setAttribute("toutiao", toutiao);
		request.setAttribute("shehui", shehui);
		request.setAttribute("guonei", guonei);
		request.setAttribute("guoji", guoji);
		request.setAttribute("yule", yule);
		request.setAttribute("tiyu", tiyu);
		request.setAttribute("junshi", junshi);
		request.setAttribute("keji", keji);
		request.setAttribute("caijing", caijing);
		request.setAttribute("shishang", shishang);
		request.setAttribute("youxi", youxi);
		request.setAttribute("qiche", qiche);
		request.setAttribute("xiaohua", xiaohua);
		request.setAttribute("jiankang", jiankang);
		request.setAttribute("jiaju", jiaju);
		request.setAttribute("xingzuo", xingzuo);
		request.setAttribute("lishi", lishi);
		request.setAttribute("zatan", zatan);
		request.setAttribute("category", 18);
		request.setAttribute("news", news);
		modelAndView.setViewName("jsp/content_xinwenliebiao");
		return modelAndView;
	}
	
	@RequestMapping("/selectNews")
	public ModelAndView selectNews(HttpServletRequest request, String title, String from,
			Integer curPage, String date,@Param("category")Integer category) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
		
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
	
		// choose = 2 默认分页查询
		Integer choose = 2;
		if (from != null && !from.equals("") || title != null && !title.equals("")
				|| date != null && !date.equals("") || category !=null ) {
			choose = 1;
		}	
		
		if(category !=null ) {
			if(category == 18) {
				choose = 2;
			}
		}

		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		
		Integer toutiao = null;
		Integer shehui = null;
		Integer guonei = null;
		Integer guoji = null;
		Integer yule = null;
		Integer tiyu = null;
		Integer junshi = null;
		Integer keji = null;
		Integer caijing = null;
		Integer shishang = null;
		Integer youxi = null;
		Integer qiche = null;
		Integer xiaohua = null;
		Integer jiankang = null;
		Integer jiaju = null;
		Integer xingzuo = null;
		Integer lishi = null;
		Integer zatan = null;
		NewsAllocation newsAllocation = newsAllocationService.selectByParentid(adminUserId);
		
		//根据分类查询
		if(choose == 1) {
			switch (category) {
			case 0:
				toutiao = 0;
				break;
			case 1:
				shehui = 1;
				break;
            case 2:
            	guonei = 2;
				break;
            case 3:
            	guoji = 3;
				break;
            case 4:
            	yule = 4;
				break;
            case 5:
            	tiyu = 5;
				break;
            case 6:
            	junshi = 6;
				break;
            case 7:
            	keji = 7;
				break;
            case 8:
            	caijing = 8;
				break;
            case 9:
            	shishang = 9;
				break;
            case 10:
            	youxi = 10;
				break;
            case 11:
            	qiche = 11;
				break;
            case 12:
            	xiaohua = 12;
	            break;
            case 13:
            	jiankang = 13;
	            break;
            case 14:
            	jiaju = 14;
	            break;
            case 15:
            	xingzuo = 15;
	            break;
            case 16:
            	lishi = 16;
	            break;
            case 17:
            	zatan = 17;
	            break;
			default:
				break;
			}
		}
		String dateFrom = null;
		String dateTo = null;
		String fTitle = null;
		String fFrom = null;
		//根据标题查询
		if(title != null && title.length()>0) {
			fTitle = title;
		}
		//根据来源查询
		if(from != null && from.length()>0) {
			fFrom = from;
		}
		//根据时间查询
		if(date != null && date.length()>0) {
			dateFrom = date+" 00:00:00";
			dateTo = date+" 23:59:59";
		}
		Page page = new Page();
		page.setCurrentPageNo(curPage);
		List<News> news = null;
		
		//多条件查询
		page.setRecordTotal(newsService.countByCategory(toutiao, shehui, guonei, guoji,
				yule, tiyu, junshi, keji, caijing, shishang, youxi, qiche, xiaohua,
				jiankang, jiaju, xingzuo, lishi, zatan, fTitle, fFrom, dateFrom, dateTo));
		news = newsService.selectByCategory(page, toutiao, shehui, guonei, guoji,
				yule, tiyu, junshi, keji, caijing, shishang, youxi, qiche, 
				xiaohua, jiankang, jiaju, xingzuo, lishi, zatan, fTitle, fFrom, dateFrom, dateTo);
		
		//查询已添加的分类
		if(newsAllocation != null) {
			toutiao = 0;
			if(newsAllocation.getShehui() == 1) {
				shehui = 1;
			}
			if(newsAllocation.getGuonei() == 1) {
				guonei = 2;
			}
			if(newsAllocation.getGuoji() == 1) {
				guoji = 3;
			}
		    if(newsAllocation.getYule() == 1) {
				yule = 4;
			}
		    if(newsAllocation.getTiyu() == 1) {
				tiyu = 5;
			}
		    if(newsAllocation.getJunshi() == 1) {
				junshi = 6;
			}
		    if(newsAllocation.getKeji() == 1) {
				keji = 7;
			}
		    if(newsAllocation.getCaijing() == 1) {
				caijing = 8;
			}
		    if(newsAllocation.getShishang() == 1) {
				shishang = 9;
			}
		    if(newsAllocation.getYouxi() == 1) {
				youxi = 10;
			}
		    if(newsAllocation.getQiche() == 1) {
				qiche = 11;
			}
		    if(newsAllocation.getXiaohua() == 1) {
				xiaohua = 12;
			}
		    if(newsAllocation.getJiankang() == 1) {
				jiankang = 13;
			}
		    if(newsAllocation.getJiaju() == 1) {
		    	jiaju = 14;
			}
		    if(newsAllocation.getXingzuo() == 1) {
		    	xingzuo = 15;
			}
		    if(newsAllocation.getLishi() == 1) {
		    	lishi = 16;
			}
		    if(newsAllocation.getZatan() == 1) {
		    	zatan = 17;
			}
		}
		//分页查询
		if (choose == 2) {
			page.setRecordTotal(newsService.countByCategory(toutiao, shehui, guonei, guoji, yule, tiyu, 
					junshi, keji, caijing, shishang, youxi, qiche, xiaohua, 
					jiankang, jiaju, xingzuo, lishi, zatan, fTitle, fFrom, dateFrom, dateTo));
			news = newsService.selectByCategory(page, toutiao, shehui, guonei, guoji, yule,
					tiyu, junshi, keji, caijing, shishang, youxi, qiche, xiaohua, 
					jiankang, jiaju, xingzuo, lishi, zatan, fTitle, fFrom, dateFrom, dateTo);
		}
		
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

		if (news != null) {
			for(News n : news) {
				switch (n.getCategory()) {
				case 0:
					n.setType("头条");
					break;
				case 1:
					n.setType("社会");
					break;
				case 2:
					n.setType("国内");
					break;
				case 3:
					n.setType("国际");
					break;
				case 4:
					n.setType("娱乐");
					break;
				case 5:
					n.setType("体育");
					break;
				case 6:
					n.setType("军事");
					break;
				case 7:
					n.setType("科技");
					break;
				case 8:
					n.setType("财经");
					break;
				case 9:
					n.setType("时尚");
					break;
				case 10:
					n.setType("电竞");
					break;
				case 11:
					n.setType("汽车");
					break;
				case 12:
					n.setType("笑话");
					break;
				case 13:
					n.setType("健康");
					break;
				case 14:
					n.setType("生活");
					break;
				case 15:
					n.setType("星座");
					break;
				case 16:
					n.setType("历史");
					break;
				case 17:
					n.setType("杂谈");
					break;
				default:
					break;
				}
			}
		}
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/content_xinwenliebiao");
		request.setAttribute("title", title);
		request.setAttribute("from", from);
		request.setAttribute("date", date);
		request.setAttribute("category", category);
		request.setAttribute("toutiao", toutiao);
		request.setAttribute("shehui", shehui);
		request.setAttribute("guonei", guonei);
		request.setAttribute("guoji", guoji);
		request.setAttribute("yule", yule);
		request.setAttribute("tiyu", tiyu);
		request.setAttribute("junshi", junshi);
		request.setAttribute("keji", keji);
		request.setAttribute("caijing", caijing);
		request.setAttribute("shishang", shishang);
		request.setAttribute("youxi", youxi);
		request.setAttribute("qiche", qiche);
		request.setAttribute("xiaohua", xiaohua);
		request.setAttribute("jiankang", jiankang);
		request.setAttribute("jiaju", jiaju);
		request.setAttribute("xingzuo", xingzuo);
		request.setAttribute("lishi", lishi);
		request.setAttribute("zatan", zatan);
		request.setAttribute("news", news);
		
		return modelAndView;
	}
}
